package com.cn.yunhe.rentcars.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.captcah.BusiLogUtil;
import com.cn.yunhe.rentcars.mapper.BusiLogMapper;
import com.cn.yunhe.rentcars.mapper.MemberLevelMapper;
import com.cn.yunhe.rentcars.model.MemberLevel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 会员等级控制
 * 
 * @author 朱钦明
 *
 */

@RestController
public class GradeController {
	
	@Autowired
	private MemberLevelMapper memberLevelMapper;
	
	/**
	 * @author 朱钦明添加
	 * 注入日志mapper
	 */
	@Autowired
	private BusiLogMapper busiLogMapper;
	
	/**
	 *  页面跳转
	 * @return 返回视图跳转到gradelist页面
	 */
	@RequestMapping(value="/gradelist",method=RequestMethod.GET)
	public ModelAndView gradeList(){
		
		return new ModelAndView("gradelist");
	}
	
	/**
	 * 添加会员等级页面跳转
	 * @return 返回视图跳转到gradeadd页面
	 */
	@RequestMapping(value="/gradeadd",method=RequestMethod.GET)
	public ModelAndView gradeAdd(){
		
		return new ModelAndView("gradeadd");
	}
	
	/**
	 * 修改会员等级页面跳转
	 * @return 返回视图跳转到gradeedit页面
	 */
	@RequestMapping(value="/gradeedit",method=RequestMethod.GET)
	public ModelAndView gradeEdit(@RequestParam String id){
		MemberLevel memberLevel = memberLevelMapper.getMemberLevelById(id);
		
		Map<String,MemberLevel> map = new HashMap<String,MemberLevel>();
		map.put("memberLevel", memberLevel);
		return new ModelAndView("gradeedit",map);
	}
	
	/**
	 * 跳转到查看会员等级页面gradeget
	 * @return ModelAndView 跳转到gradeget页面
	 */
	@RequestMapping(value="/gradeget",method=RequestMethod.GET)
	public ModelAndView gradeGet(@RequestParam String id){
		MemberLevel memberLevel = memberLevelMapper.getMemberLevelById(id);
		
		/*
		 * 把时间格式化从字符串形式
		 */
		Date date =memberLevel.getAddTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdf.format(date);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("memberLevel", memberLevel);
		map.put("dateStr", dateStr);
		return new ModelAndView("gradeget",map);
	}
	
	/**
	 * 查询所有grade信息
	 * @return 返回给页面一个map集合
	 * @param draw
	 * @param start
	 * @param length
	 */
	@RequestMapping(value="/grades",method=RequestMethod.GET)
	public Map<String,Object> gradeMap(@RequestParam int draw,@RequestParam int start,@RequestParam int length){
		int pageNum = (start/length)+1;
		int pageSize = length;
		
		PageHelper.startPage(pageNum, pageSize);
		List<MemberLevel> memberList = memberLevelMapper.getMemberLevels();
		
		PageInfo<MemberLevel> page = new PageInfo<MemberLevel>(memberList);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("draw", draw);
		map.put("data", page.getList());
		map.put("recordsTotal", page.getTotal());
		map.put("recordsFiltered", page.getTotal());
		return map;
	}
	
	/**
	 * 添加grade信息
	 * @return void
	 * @param MemberLevel
	 */
	@RequestMapping(value="/addgrades",method=RequestMethod.POST)
	public void gradeAdds(@RequestBody MemberLevel memberLevel,HttpServletRequest request){
		memberLevel.setLevelId(UUID.randomUUID().toString());
		memberLevel.setAddTime(new Date());
		memberLevelMapper.addGrades(memberLevel);
		//添加日志信息
		Map<String,Object> busMap = new HashMap<String,Object>();
		busMap.put("logid", UUID.randomUUID().toString());
		busMap.put("businame", "添加会员等级信息");
		busMap.put("busitype", "添加");
		busMap.put("busiconent","添加会员等级信息");
		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
	}
	
	
	/**
	 * 修改grade信息
	 * @return void
	 * @param 会员等级id
	 */
	@RequestMapping(value="/editgrade/{id}",method=RequestMethod.PUT)
	public void editGrades(@PathVariable("id") String id,@RequestBody MemberLevel memberLevel,HttpServletRequest request){
		memberLevel.setLevelId(id);
		memberLevel.setAddTime(new Date());
		memberLevelMapper.updateMemberLevel(memberLevel);
		//添加日志信息
		Map<String,Object> busMap = new HashMap<String,Object>();
		busMap.put("logid", UUID.randomUUID().toString());
		busMap.put("businame", "修改会员等级信息");
		busMap.put("busitype", "修改");
		busMap.put("busiconent","修改会员等级信息");
		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
	}
	
	
	/**
	 * 删除grade信息
	 * @return void 
	 * @param levelId 对应的等级编号
	 */
	@RequestMapping(value="/deletegrade",method=RequestMethod.POST)
	public void deleteGrade(@RequestParam String id,HttpServletRequest request){
		memberLevelMapper.deleteMemberLevel(id);
		//添加日志信息
		Map<String,Object> busMap = new HashMap<String,Object>();
		busMap.put("logid", UUID.randomUUID().toString());
		busMap.put("businame", "删除会员等级信息");
		busMap.put("busitype", "删除");
		busMap.put("busiconent","删除会员等级信息");
		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
	}
	
	
	
	/**
	 * 批量删除grade信息
	 * @return void
	 * @param levelids 传入的是一个字符串，然后进行切割
	 */
	@RequestMapping(value="/deletegrades",method=RequestMethod.POST)
	public void deleteGrades(@RequestParam String levelIds,HttpServletRequest request){
		String[] ids = levelIds.split(",");
		for(String id : ids){
			memberLevelMapper.deleteMemberLevel(id);
			//添加日志信息
			Map<String,Object> busMap = new HashMap<String,Object>();
			busMap.put("logid", UUID.randomUUID().toString());
			busMap.put("businame", "删除会员等级信息");
			busMap.put("busitype", "删除");
			busMap.put("busiconent","删除会员等级信息");
			BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
		}
	}
	
}
